package com.lam.system.service.impl;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.dict.DictCacheUtils;
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import com.lam.common.constant.SystemConstant;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.bean.entity.SysDict;
import com.lam.system.entity.imp.SysDictImp;
import com.lam.system.mapper.SysDictItemMapper;
import com.lam.system.mapper.SysDictMapper;
import com.lam.system.param.SysDictParam;
import com.lam.system.service.SysDictService;

/**
 * 字典配置Service业务层处理
 * 
 * @author admin
 * @date 2022-09-19
 */
@Service
public class SysDictServiceImpl extends AbstractImportService<SysDictImp, SysDict> implements SysDictService {

    @Autowired
    private SysDictMapper sysDictMapper;
    @Autowired
    private SysDictItemMapper sysDictItemMapper;

    /**
     * 查询字典配置
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysDict selectById(String id) {

        return sysDictMapper.selectById(id);
    }

    /**
     * 查询字典配置列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysDict> selectList(SysDictParam param) {
    
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
        	queryWrapper.and(wr->{
        		wr.or().like("dict_name", param.getKeywords());
        		wr.or().like("dict_code", param.getKeywords());
        	});
        }
        
        //keywords
        queryWrapper.like(StringUtils.isNotBlank(param.getDictNameKey()), "dict_name", param.getDictNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDictName()), "dict_name", param.getDictName());
        queryWrapper.like(StringUtils.isNotBlank(param.getDictCodeKey()), "dict_code", param.getDictCodeKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getDictCode()), "dict_code", param.getDictCode());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.eq(Objects.nonNull(param.getType()), "type", param.getType());

        return sysDictMapper.selectList(queryWrapper);
    }

    /**
     * 新增字典配置
     * 
     * @param sysDict 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysDict sysDict) {
    
    	int n = sysDictMapper.insert(sysDict);
		DictCacheUtils.delDictCache(sysDict.getDictCode());
        return n;
    }

    /**
     * 修改字典配置
     * 
     * @param sysDict 
     * @return 
     */
    @Override
    @Transactional
    public int update(SysDict sysDict) {
    
    	int n = sysDictMapper.updateById(sysDict);
		DictCacheUtils.delDictCache(sysDict.getDictCode());
        return n;
    }

    /**
     * 删除字典配置信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	int n = 0;
    	SysDict sysDict;
    	for (String id : StringUtils.str2List(idStr)) {
    		sysDict = selectById(id);
            n += sysDictMapper.deleteById(id);
            sysDictItemMapper.deleteByDictId(id);
            if(sysDict != null) {
            	DictCacheUtils.delDictCache(sysDict.getDictCode());
            }
        }
        return n;
    }
    
    @Override
    @Transactional
    public void importData(ImportWrapper<SysDictImp, SysDict> wrapper) {
        
    	UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(SystemConstant.UNIQUE_PARAMETER_CODE);
    	importData(wrapper, new AbstractImportBody<SysDictImp, SysDict>() {
			@Override
			public SysDict selectRecord(ImportEntry<SysDictImp, SysDict> entry) {
				return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, SysDict.class);
			}
			@Override
			public int insertData(SysDict data) {
				return insert(data);
			}
			@Override
			public int updateData(SysDict data) {
				return update(data);
			}
			@Override
			public String notUniqueMsg(ImportEntry<SysDictImp, SysDict> entry) {
				return MessageFormat.format("字典编码【{0}】已存在", entry.getData().getDictCode());
			}
    	});
    }
    
}
